#ifndef PARAMS_H
#define PARAMS_H

#include <QObject>
#include <QString>
#include <QSettings>
#include <QMap>


// Bit mask constants
enum PPARAMS_PROP {
    BM_PORTNAME       = 0x00000001,
    BM_EMUBR          = 0x00000002,
    BM_EMUOVERRUN     = 0x00000004,
    BM_PLUGINMODE     = 0x00000008,
    BM_EXCLUSIVEMODE  = 0x00000010,
    BM_HIDDENMODE     = 0x00000020,
    BM_ALLDATABITS    = 0x00000040,
    BM_PINCTS         = 0x00000100,
    BM_PINDSR         = 0x00000200,
    BM_PINDCD         = 0x00000400,
    BM_PINRI          = 0x00000800,
    BM_EMUNOISE       = 0x00010000,
    BM_ADDRTTO        = 0x00020000,
    BM_ADDRITO        = 0x00040000,
    BM_REALPORTNAME   = 0x01000000,
};

struct Bit;

/**
 * @brief 端口参数类
 * 该类用于管理串口端口的参数，包括波特率、数据位、停止位、校验位等。
 * 通常用于在设备枚举过程中把 PortParameters 指针传给回调（如 LoadRealPortName ）
 * 由回调在匹配到目标设备时写入 RealPortName 与相关位掩码。
 */
class PortParameters : public QObject
{
    Q_OBJECT

public:
    explicit PortParameters(const QString &service, const QString &physicalPortName, QObject *parent = nullptr);

    void init();
    bool load();
    bool save();
    bool initRealPortName(const QString &realPortName = QString());
    bool parseParametersString(const QString &parameters);
    QString getParametersString(bool detail = false);
    QString getPortName() const;
    QString getRealPortName() const;
    bool hasChanged() const { return m_maskChanged != 0; }

    static QString getHelp();

signals:
    void parametersChanged();

protected:
    QString getParametersKey() const;
    quint32 *getDwordPointer(quint32 bit);
    void loadDword(QSettings &settings, quint32 bit);
    bool saveDword(QSettings &settings, quint32 bit);

    bool setPortName(const QString &portName);
    bool setRealPortName(const QString &newRealPortName);
    bool setFlag(const QString &newValue, quint32 bit);
    bool setPin(const QString &newValue, quint32 bit);
    bool setProbability(const QString &newValue, quint32 bit);
    bool setUnsigned(const QString &newValue, quint32 bit);
    bool setBit(const QString &value, const Bit &bit);

private:
    quint32 m_maskChanged;
    quint32 m_maskExplicit;
    QString m_portName;
    QString m_realPortName;
    quint32 m_emuBR;
    quint32 m_emuOverrun;
    quint32 m_plugInMode;
    quint32 m_exclusiveMode;
    quint32 m_hiddenMode;
    quint32 m_allDataBits;
    quint32 m_pinCTS;
    quint32 m_pinDSR;
    quint32 m_pinDCD;
    quint32 m_pinRI;
    quint32 m_emuNoise;
    quint32 m_addRTTO;
    quint32 m_addRITO;

    QString m_service;
    QString m_physicalPortName;

    static const QMap<quint32, QString> s_bitNames;
    static QMap<quint32, QString> initBitNames();
};

#endif // PARAMS_H
